<?
    function compare_term($target, $target_list)
{
$term_exist = false;

for ($i = 0; $i < count($target_list); $i++) {
if ($target == $target_list[$i]) {
$term_exist = true;
}
}

return $term_exist;
}
?>

<?php
    $brand_result   = mysql_query("select * from " . $table_Prefix . "term where belong = 'brand' and interm = 'root' and publish = 1 order by CONVERT(name using gbk) asc ");
    $brand_term_nid = explode(',', $term_info['shop_brand']);

?>


<div id="brand_list"></div>


<script>

    layui.use('tree', function () {

        var tree = layui.tree;


        //渲染

        var inst1 = tree.render({

            elem: '#brand_list'  //绑定元素

            , showCheckbox: true

            , id: 'brand_list'

            , data: [
                {
                    title: '全选',
                    id: '全选',
                    checked: false
                },


                <?php

                $i = 0;

                while ($brand = mysql_fetch_array($brand_result)) { //一级

                if ($i > 0) {
                    echo ',';
                }

                ?>

                {
                    title: '<?php echo $brand['name']?>'

                    , id: <?php echo $brand['nid']?>

                    , field: ''

                    <?

                    if (compare_term($brand['nid'], $brand_term_nid)) {

                    ?>

                    , checked: true

                    , spread: true

                    <?php

                    }



                    $i++;

                    $brand2_result = mysql_query("select * from " . $table_Prefix . "term where belong = 'brand' and interm = 'term1' and publish = 1 and term1 =  " . $brand['nid']);

                    if($brand2_result){

                    ?>

                    , children: [

                        <?php

                        $ii = 0;

                        while ($brand2 = mysql_fetch_array($brand2_result)) { //二级

                        if ($ii > 0) {
                            echo ',';
                        }

                        ?>

                        {
                            title: '<?php echo $brand2['name']?>'

                            , id: <?php echo $brand2['nid']?>

                            , field: ''

                            <?

                            if (compare_term($brand2['nid'], $brand_term_nid)) {

                            ?>

                            , checked: true

                            , spread: true

                            <?php

                            }

                            $ii++;

                            $brand3_result = mysql_query("select * from " . $table_Prefix . "term where belong = 'brand' and interm = 'term2' and publish = 1 and term2 =  " . $brand2['nid']);



                            if($brand3_result){

                            ?>

                            , children: [

                                <?php

                                $iii = 0;

                                while ($brand3 = mysql_fetch_array($brand3_result)) { //三级



                                if ($iii > 0) {
                                    echo ',';
                                }

                                ?>

                                {
                                    title: '<?php echo $brand3['name']?>'

                                    , id: <?php echo $brand3['nid']?>

                                    , field: ''

                                    <?

                                    if (compare_term($brand3['nid'], $brand_term_nid)) {

                                    ?>

                                    , checked: true

                                    , spread: true

                                    <?php

                                    }

                                    ?>

                                }

                                <?php



                                $iii++;

                                }

                                ?>



                            ]

                        }

                        <?php

                        }

                        }

                        ?>

                    ]
                }

                <?php

                }

                }

                ?>



            ],
            oncheck: function (obj) {
                let title = obj.data.title
                if (title === '全选') {
                    let data = inst1.config.data
                    let ids = []
                    data.forEach(function (item) {
                        item.id && ids.push(item.id)
                    })
                    if (obj.checked) {
                        // 勾中全选
                        tree.setChecked('brand_list', ids)
                    } else {
                        // 取消全选
                        let data = inst1.config.data
                        data.forEach(function (item) {
                            if (item.hasOwnProperty('checked')) {
                                item.checked = false
                            }
                        })
                        inst1.reload()
                    }
                }
            }

        });

        // 所有已选中的 checkbox 数量
        let checkedDataLen = inst1.getChecked('brand_list').length
        // 总 checkbox 数量，需减去一个 '全选' checkbox
        let allDataLen = inst1.config.data.length - 1

        if (checkedDataLen === allDataLen) {
            // 全部已选中，更新 '全选' checkbox 的状态为 true
            tree.setChecked('brand_list', ['全选'])
        }

    });


</script>



       